//
//  CLDocQuickShareCell.m
//  chanlin
//
//  Created by shuhui on 15-6-13.
//  Copyright (c) 2015年 chanlin. All rights reserved.
//

#import "CLDocQuickShareCell.h"
#import "CLGlobleDataModel.h"
@interface CLDocQuickShareCell ()
@property (nonatomic, strong) NSArray *quickDataArray_Day;

@end

@implementation CLDocQuickShareCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self _createItems];
        _document = [CLDocumentModel new];
    }
    return self;
}

- (void)_createItems {
    
    UIImageView *leftLineImg = [[UIImageView alloc]initWithFrame:CGRectMake(12, 5, 124, 0.5)];
    leftLineImg.backgroundColor = RGB(0xe0, 0xe0, 0xe0);
    [self addSubview:leftLineImg];
    
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(leftLineImg.frame.origin.x+leftLineImg.frame.size.width+3, 0, 36, 12)];
    label.text = @"分享到";
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = RGB(0x56, 0x56, 0x5e);
    label.font = [UIFont systemFontOfSize:12];
    [self addSubview:label];
    
    UIImageView *rightLineImg = [[UIImageView alloc]initWithFrame:CGRectMake(label.frame.origin.x+label.frame.size.width+3, 5, 124, 0.5)];
    rightLineImg.backgroundColor = RGB(0xe0, 0xe0, 0xe0);
    [self addSubview:rightLineImg];
    
    _quickDataArray_Day = @[@{@"image":@"doc_quick_weichat.png"},@{@"image":@"doc_quick_friend.png"},@{@"image":@"doc_quick_weibo.png"}];
    
    [self _createButtonsWithFrame:CGRectMake(44, 28, 48, 48) WithTag:200];
    [self _createButtonsWithFrame:CGRectMake(44*2+48, 28, 48, 48) WithTag:201];
    [self _createButtonsWithFrame:CGRectMake(44*3+48*2, 28, 48, 48) WithTag:202];
    
    [self _createTitleWithFrame:CGRectMake(44, 28+48+8, 48, 12) withTitle:@"微信"];
    [self _createTitleWithFrame:CGRectMake(44*2+48, 28+48+8, 48, 12) withTitle:@"朋友圈"];
    [self _createTitleWithFrame:CGRectMake(44*3+48*2, 28+48+8, 48, 12) withTitle:@"微博"];
}

- (void)_createButtonsWithFrame:(CGRect)_frame WithTag:(NSInteger)_buttonTag
{
    UIButton *quickButton = [UIButton buttonWithType:UIButtonTypeCustom];
    quickButton.backgroundColor = [UIColor clearColor];
    [quickButton setFrame:_frame];
    quickButton.adjustsImageWhenDisabled = YES;
    [quickButton setTag:_buttonTag];
    
    [quickButton setBackgroundImage:[UIImage imageNamed:[_quickDataArray_Day[(_buttonTag-200)] sgrGetStringForKey:@"image"]] forState:UIControlStateNormal];
     
        [quickButton setBackgroundImage:[UIImage imageNamed:[_quickDataArray_Day[(_buttonTag-200)] sgrGetStringForKey:@"image"]] forState:UIControlStateHighlighted];
        
    [quickButton addTarget:self action:@selector(didSelectQuickShare:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:quickButton];
}

- (void)_createTitleWithFrame:(CGRect)_frame withTitle:(NSString *)_title {
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.frame = _frame;
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.text = _title;
    titleLabel.font = [UIFont systemFontOfSize:12.f];
    titleLabel.tintColor = RGB(0x54, 0x54, 0x54);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:titleLabel];
}

-(UIImage *)getImageFromURL:(NSString *)fileURL {
    UIImage * result;
    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
    result = [UIImage imageWithData:data];
    if (result) {
        return result;
    }else
        return [UIImage imageNamed:@"icon114.png"];
    
}

- (void)didSelectQuickShare:(UIButton *)button {
    
    switch (button.tag) {
        case 200:
            [[CLGlobleDataModel sharedInstance] share2Wechat:[self getImageFromURL:_document.entity.sharepicUrl] title:_document.entity.title content:nil url:_document.entity.shareUrl captureImg:nil uiBlock:nil];
            break;
        case 201:
            [[CLGlobleDataModel sharedInstance] share2pengyou:[self getImageFromURL:_document.entity.sharepicUrl] title:_document.entity.title content:nil url:_document.entity.shareUrl captureImg:nil uiBlock:nil];
            break;
        case 202:
            [[CLGlobleDataModel sharedInstance] share2Sina:_document.entity.title url:_document.entity.shareUrl controller:(UIViewController *)[self k_currentController] captureImage:nil uiBlock:nil];
            break;
            
        default:
            break;
    }

}

@end
